package com.landinfo.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.landinfo.service.PatrolRecordService;
import com.landinfo.web.rest.util.HeaderUtil;
import com.landinfo.web.rest.util.PaginationUtil;
import com.landinfo.service.dto.PatrolRecordDTO;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing PatrolRecord.
 */
@RestController
@RequestMapping("/api")
public class PatrolRecordResource {

    private final Logger log = LoggerFactory.getLogger(PatrolRecordResource.class);

    private static final String ENTITY_NAME = "patrolRecord";

    private final PatrolRecordService patrolRecordService;

    public PatrolRecordResource(PatrolRecordService patrolRecordService) {
        this.patrolRecordService = patrolRecordService;
    }

    /**
     * POST  /patrol-records : Create a new patrolRecord.
     *
     * @param patrolRecordDTO the patrolRecordDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new patrolRecordDTO, or with status 400 (Bad Request) if the patrolRecord has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/patrol-records")
    @Timed
    public ResponseEntity<PatrolRecordDTO> createPatrolRecord(@RequestBody PatrolRecordDTO patrolRecordDTO) throws URISyntaxException {
        log.debug("REST request to save PatrolRecord : {}", patrolRecordDTO);
        if (patrolRecordDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new patrolRecord cannot already have an ID")).body(null);
        }
        PatrolRecordDTO result = patrolRecordService.save(patrolRecordDTO);
        return ResponseEntity.created(new URI("/api/patrol-records/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /patrol-records : Updates an existing patrolRecord.
     *
     * @param patrolRecordDTO the patrolRecordDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated patrolRecordDTO,
     * or with status 400 (Bad Request) if the patrolRecordDTO is not valid,
     * or with status 500 (Internal Server Error) if the patrolRecordDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/patrol-records")
    @Timed
    public ResponseEntity<PatrolRecordDTO> updatePatrolRecord(@RequestBody PatrolRecordDTO patrolRecordDTO) throws URISyntaxException {
        log.debug("REST request to update PatrolRecord : {}", patrolRecordDTO);
        if (patrolRecordDTO.getId() == null) {
            return createPatrolRecord(patrolRecordDTO);
        }
        PatrolRecordDTO result = patrolRecordService.save(patrolRecordDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, patrolRecordDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /patrol-records : get all the patrolRecords.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of patrolRecords in body
     */
    @GetMapping("/patrol-records")
    @Timed
    public ResponseEntity<List<PatrolRecordDTO>> getAllPatrolRecords(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of PatrolRecords");
        Page<PatrolRecordDTO> page = patrolRecordService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/patrol-records");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /patrol-records/:id : get the "id" patrolRecord.
     *
     * @param id the id of the patrolRecordDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the patrolRecordDTO, or with status 404 (Not Found)
     */
    @GetMapping("/patrol-records/{id}")
    @Timed
    public ResponseEntity<PatrolRecordDTO> getPatrolRecord(@PathVariable Long id) {
        log.debug("REST request to get PatrolRecord : {}", id);
        PatrolRecordDTO patrolRecordDTO = patrolRecordService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(patrolRecordDTO));
    }

    /**
     * DELETE  /patrol-records/:id : delete the "id" patrolRecord.
     *
     * @param id the id of the patrolRecordDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/patrol-records/{id}")
    @Timed
    public ResponseEntity<Void> deletePatrolRecord(@PathVariable Long id) {
        log.debug("REST request to delete PatrolRecord : {}", id);
        patrolRecordService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
